/*
 * nmeRecArray.cpp
 *
 *  Created on: Jan 3, 2009
 *      Author: Carmine Red
 */

#include "nmeRecArray.h"


nmeRecArray::nmeRecArray()
{
	num_enemies = 0;
}

nmeRecArray::~nmeRecArray()
{
}

int nmeRecArray::getIndexFromID(int id)		// return an index to the enemy with
								// this id, -1 if not found
{
	for(int i = 0; i < num_enemies; ++i)
	{
		if(nme_ID[i] == id)
		{
			// success!
			return i;
		}
	}
	// if we get here we couldn't find it
	return -1;
}

bool nmeRecArray::setnmeNew(int id, bool value)
{
	int index = getIndexFromID(id);
	if(index == -1)
	{
		return false;
	}
	else
	{
		nme_New[index] = value;
		return true;
	}
}

bool nmeRecArray::getnmeNew(int id)
{
	int index = getIndexFromID(id);
	if(index == -1)
	{
		return false;
	}
	return nme_New[index];
}

int nmeRecArray::getnmeID(int index)	// gets id of enemy at index
{
	if(index < 0 || index > num_enemies)
	{
		return -1;
	}
	return nme_ID[index];
}

bool nmeRecArray::setnmeChanged(int id, bool value)
{
	int index = getIndexFromID(id);
	if(index == -1)
	{
		return false;
	}
	else
	{
		nme_Changed[index] = value;
		return true;
	}
}

bool nmeRecArray::getnmeChangedViaID(int id)		// gets changed status of enemy with this id
{
	int index = getIndexFromID(id);
	if(index == -1)
	{
		return false;
	}
	else
	{
		return nme_Changed[index];
	}
}

bool nmeRecArray::getnmeChangedViaIndex(int index)	// gets changed status of enemy at this index
{
	if(index < 0 || index > num_enemies)
	{
		return -1;
	}
	return nme_Changed[index];
}

bool nmeRecArray::setnmeAboveStatus(int id, bool value)	/*above == true, below = false*/
{
	int index = getIndexFromID(id);
	if(index == -1)
	{
		return false;
	}
	else
	{
		nme_Status[index] = value;
		return true;
	}
}

bool nmeRecArray::getnmeAboveStatus(int id)	// returns false if id not found
{
	int index = getIndexFromID(id);
	if(index == -1)
	{
		// id not found
		return false;
	}
	else
	{
		return nme_Status[index];
	}
}

bool nmeRecArray::addRecordnme(int id)	// returns false if no more space
							// otherwise, increments num_enemies, and uses the
							// 		given id, status is below(false),
							// 		New is true, changed is false
{
	if(num_enemies == MAX_ENEMIES)
	{
		// no more room left
		return false;
	}
	else
	{
		nme_ID[num_enemies] = id;
		nme_Status[num_enemies] = false; // below
		nme_New[num_enemies] = true;
		nme_Changed[num_enemies] = false;
		++num_enemies;
		return true;
	}
}

bool nmeRecArray::removeViaID(int id)	// returns false if can't find id
							// otherwise switches record with the last record in the
							// array, decrements num_enemies by 1
{
	int index = getIndexFromID(id);
	if(index == -1)
	{
		// id not found
		return false;
	}
	else
	{
		int temp_ID;
		bool temp_Status;
		bool temp_New;
		bool temp_Changed;

		//switch with last record

		temp_ID = nme_ID[num_enemies];
		temp_Status = nme_Status[num_enemies];
		temp_New = nme_New[num_enemies];
		temp_Changed = nme_Changed[num_enemies];

		nme_ID[num_enemies] = nme_ID[index];
		nme_Status[num_enemies] = nme_Status[index];
		nme_New[num_enemies] = nme_New[index];
		nme_Changed[num_enemies] = nme_Changed[index];

		nme_ID[index] = temp_ID;
		nme_Status[index] = temp_Status;
		nme_New[index] = temp_New;
		nme_Changed[index] = temp_Changed;

		--num_enemies;

		return true;
	}
}

bool nmeRecArray::isInStruct(int id)	// returns true if the enemy has a record in the struct
							// else returns false
{
	for(int i = 0; i < num_enemies; ++i)
	{
		if(nme_ID[i] == id)
		{
			// success!
			return true;
		}
	}
	// if we get here we couldn't find it
	return false;
}
